﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Abp.Linq;
using Abp.Linq.Extensions;
using Abp.AutoMapper;
using Abp.Application.Services;
using Abp.Extensions;
using Abp.Domain.Repositories;
using Sunlit.Business.Wishlists.Dto;
using Abp.Runtime.Session;
using Abp.UI;

namespace Sunlit.Business.Wishlists
{
    public class WishlistAppService:AsyncCrudAppService<Wishlist, WishlistDto,
        int,GetAllWishlistInput,CreateWishlistInput,UpdateWishlistInput>,IWishlistAppService
    {
        public WishlistAppService(IRepository<Wishlist> repository):base(repository)
        {

        }
        protected override IQueryable<Wishlist> CreateFilteredQuery(GetAllWishlistInput input)
        {
            return base.CreateFilteredQuery(input)
                .WhereIf(input.CreatorUserId.HasValue, w => w.CreatorUserId == input.CreatorUserId)
                .WhereIf(!input.Title.IsNullOrEmpty(), w => w.Title.Contains(input.Title));
        }
        /// <summary>
        /// 是否已经收藏
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool IsWished(IsWishedInput input)
        {
            try
            {
                var userId = AbpSession.GetUserId();
                return Repository.GetAll()
                .Any(w => w.SliderType == input.SliderType && w.LinkUrl == input.ArticleId.ToString()&& w.CreatorUserId == userId);
            }
            catch 
            {

                throw new UserFriendlyException("你还没有登录哦！");
            }
            
        }
        public WishlistDto GetMyWish(IsWishedInput input)
        {
            try
            {
                var userId = AbpSession.GetUserId();
                var wish =  Repository.GetAll()
                .FirstOrDefault(w => w.SliderType == input.SliderType && w.LinkUrl == input.ArticleId.ToString() && w.CreatorUserId == userId);
                return ObjectMapper.Map<WishlistDto>(wish);
            }
            catch
            {

                throw new UserFriendlyException("你还没有登录哦！");
            }
        }
    }
}
